<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>线程池拒绝策略指南 | 高级技术参考</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background-color: #f9fafb;
            color: #111827;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }
        .card-hover:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1);
        }
        .policy-icon {
            font-size: 2rem;
            color: #4f46e5;
            margin-bottom: 1rem;
        }
        .drop-cap:first-letter {
            float: left;
            font-size: 3.5rem;
            line-height: 0.8;
            margin: 0.2rem 0.5rem 0 0;
            color: #4f46e5;
            font-weight: 700;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-4 sm:px-6 lg:px-8">
        <div class="max-w-4xl mx-auto text-center">
            <h1 class="text-4xl md:text-5xl font-bold mb-6 font-serif">线程池拒绝策略完全指南</h1>
            <p class="text-xl md:text-2xl font-light mb-8 max-w-3xl mx-auto">
                深入解析五种核心拒绝策略，帮助您构建更健壮、更可靠的并发系统
            </p>
            <div class="inline-flex items-center px-4 py-2 border border-white rounded-full text-sm font-medium">
                <i class="fas fa-book-open mr-2"></i>
                高级并发编程参考
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
        <!-- Introduction -->
        <div class="mb-16">
            <div class="drop-cap bg-white p-8 rounded-xl shadow-sm card-hover">
                <p class="text-lg leading-relaxed text-gray-700">
                    线程池中的拒绝策略用于处理当线程池达到其最大容量，并且工作队列也已满时，无法接受新的任务的情况。这些策略定义了如何处理提交到线程池的额外任务。选择适当的拒绝策略对于线程池的稳定性和性能至关重要。每种策略都有其适用场景和优缺点，开发者应根据业务需求、任务重要性、系统负载等因素，选择最合适的策略。在高负载环境中，合适的拒绝策略可以有效避免系统崩溃或资源耗尽的问题，同时确保系统能够稳定运行。
                </p>
            </div>
        </div>

        <!-- Visualization -->
        <div class="mb-16 bg-white p-8 rounded-xl shadow-sm">
            <h2 class="text-2xl font-bold mb-6 font-serif text-gray-800">拒绝策略决策流程图</h2>
            <div class="mermaid">
                graph TD
                    A[新任务提交] --> B{线程池是否已满?}
                    B -->|否| C[加入队列执行]
                    B -->|是| D{队列是否已满?}
                    D -->|否| E[加入队列等待]
                    D -->|是| F[应用拒绝策略]
                    F --> G[AbortPolicy: 抛出异常]
                    F --> H[CallerRunsPolicy: 调用者执行]
                    F --> I[DiscardPolicy: 静默丢弃]
                    F --> J[DiscardOldestPolicy: 丢弃最旧]
                    F --> K[自定义策略]
            </div>
        </div>

        <!-- Policy Cards -->
        <div class="grid gap-8 md:grid-cols-2 lg:grid-cols-3 mb-12">
            <!-- AbortPolicy -->
            <div class="bg-white p-6 rounded-xl shadow-sm card-hover">
                <div class="policy-icon">
                    <i class="fas fa-exclamation-triangle"></i>
                </div>
                <h3 class="text-xl font-bold mb-3 text-gray-800">AbortPolicy (默认策略)</h3>
                <div class="text-gray-600 mb-4">
                    <p class="mb-2"><strong>描述：</strong>这是线程池的默认拒绝策略。它会抛出一个 <code>RejectedExecutionException</code> 异常来拒绝执行任务。</p>
                    <p class="mb-2"><strong>适用场景：</strong>适用于任务执行对系统的实时性要求高，且任务丢失不可接受的情况。</p>
                </div>
                <div class="flex flex-wrap gap-2">
                    <span class="px-3 py-1 bg-blue-100 text-blue-800 text-xs font-medium rounded-full">+ 直接告知失败</span>
                    <span class="px-3 py-1 bg-red-100 text-red-800 text-xs font-medium rounded-full">- 任务被丢弃</span>
                </div>
            </div>

            <!-- CallerRunsPolicy -->
            <div class="bg-white p-6 rounded-xl shadow-sm card-hover">
                <div class="policy-icon">
                    <i class="fas fa-user-cog"></i>
                </div>
                <h3 class="text-xl font-bold mb-3 text-gray-800">CallerRunsPolicy</h3>
                <div class="text-gray-600 mb-4">
                    <p class="mb-2"><strong>描述：</strong>该策略会直接在调用者线程中执行任务，而不是将其提交给线程池。</p>
                    <p class="mb-2"><strong>适用场景：</strong>适用于任务提交者希望在自身线程中处理任务的场景。</p>
                </div>
                <div class="flex flex-wrap gap-2">
                    <span class="px-3 py-1 bg-blue-100 text-blue-800 text-xs font-medium rounded-full">+ 避免任务丢失</span>
                    <span class="px-3 py-1 bg-red-100 text-red-800 text-xs font-medium rounded-full">- 增加调用者负担</span>
                </div>
            </div>

            <!-- DiscardPolicy -->
            <div class="bg-white p-6 rounded-xl shadow-sm card-hover">
                <div class="policy-icon">
                    <i class="fas fa-trash-alt"></i>
                </div>
                <h3 class="text-xl font-bold mb-3 text-gray-800">DiscardPolicy</h3>
                <div class="text-gray-600 mb-4">
                    <p class="mb-2"><strong>描述：</strong>该策略会悄无声息地丢弃任务，而不会抛出异常或通知调用者。</p>
                    <p class="mb-2"><strong>适用场景：</strong>适用于任务处理的重要性低或者任务可以丢弃的场景。</p>
                </div>
                <div class="flex flex-wrap gap-2">
                    <span class="px-3 py-1 bg-blue-100 text-blue-800 text-xs font-medium rounded-full">+ 轻量无异常</span>
                    <span class="px-3 py-1 bg-red-100 text-red-800 text-xs font-medium rounded-full">- 任务丢失</span>
                </div>
            </div>

            <!-- DiscardOldestPolicy -->
            <div class="bg-white p-6 rounded-xl shadow-sm card-hover">
                <div class="policy-icon">
                    <i class="fas fa-history"></i>
                </div>
                <h3 class="text-xl font-bold mb-3 text-gray-800">DiscardOldestPolicy</h3>
                <div class="text-gray-600 mb-4">
                    <p class="mb-2"><strong>描述：</strong>该策略会丢弃工作队列中最旧的任务，然后尝试提交新的任务。</p>
                    <p class="mb-2"><strong>适用场景：</strong>适用于系统希望保持队列的活跃状态，并愿意丢弃旧的、不再重要的任务。</p>
                </div>
                <div class="flex flex-wrap gap-2">
                    <span class="px-3 py-1 bg-blue-100 text-blue-800 text-xs font-medium rounded-full">+ 执行新任务</span>
                    <span class="px-3 py-1 bg-red-100 text-red-800 text-xs font-medium rounded-full">- 旧任务丢失</span>
                </div>
            </div>

            <!-- Custom Policy -->
            <div class="bg-white p-6 rounded-xl shadow-sm card-hover">
                <div class="policy-icon">
                    <i class="fas fa-cogs"></i>
                </div>
                <h3 class="text-xl font-bold mb-3 text-gray-800">自定义拒绝策略</h3>
                <div class="text-gray-600 mb-4">
                    <p class="mb-2"><strong>描述：</strong>开发者可以根据具体业务需求，自定义拒绝策略。</p>
                    <p class="mb-2"><strong>适用场景：</strong>适用于需要特别处理逻辑的场景，比如将任务持久化到数据库或其他存储系统。</p>
                </div>
                <div class="flex flex-wrap gap-2">
                    <span class="px-3 py-1 bg-blue-100 text-blue-800 text-xs font-medium rounded-full">+ 灵活定制</span>
                    <span class="px-3 py-1 bg-red-100 text-red-800 text-xs font-medium rounded-full">- 开发成本</span>
                </div>
            </div>
        </div>

        <!-- Comparison Table -->
        <div class="bg-white rounded-xl shadow-sm overflow-hidden mb-12">
            <div class="p-6 border-b border-gray-200">
                <h2 class="text-2xl font-bold font-serif text-gray-800">拒绝策略对比分析</h2>
            </div>
            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-gray-200">
                    <thead class="bg-gray-50">
                        <tr>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">策略名称</th>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">行为</th>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">适用场景</th>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">优点</th>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">缺点</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200">
                        <tr>
                            <td class="px-6 py-4 whitespace-nowrap font-medium text-gray-900">AbortPolicy</td>
                            <td class="px-6 py-4 text-gray-500">抛出异常</td>
                            <td class="px-6 py-4 text-gray-500">任务丢失不可接受</td>
                            <td class="px-6 py-4 text-green-600">直接告知失败</td>
                            <td class="px-6 py-4 text-red-600">任务被丢弃</td>
                        </tr>
                        <tr>
                            <td class="px-6 py-4 whitespace-nowrap font-medium text-gray-900">CallerRunsPolicy</td>
                            <td class="px-6 py-4 text-gray-500">调用者执行</td>
                            <td class="px-6 py-4 text-gray-500">防止任务丢失</td>
                            <td class="px-6 py-4 text-green-600">避免任务丢失</td>
                            <td class="px-6 py-4 text-red-600">增加调用者负担</td>
                        </tr>
                        <tr>
                            <td class="px-6 py-4 whitespace-nowrap font-medium text-gray-900">DiscardPolicy</td>
                            <td class="px-6 py-4 text-gray-500">静默丢弃</td>
                            <td class="px-6 py-4 text-gray-500">任务可丢弃</td>
                            <td class="px-6 py-4 text-green-600">轻量无异常</td>
                            <td class="px-6 py-4 text-red-600">任务丢失</td>
                        </tr>
                        <tr>
                            <td class="px-6 py-4 whitespace-nowrap font-medium text-gray-900">DiscardOldestPolicy</td>
                            <td class="px-6 py-4 text-gray-500">丢弃最旧任务</td>
                            <td class="px-6 py-4 text-gray-500">保持队列活跃</td>
                            <td class="px-6 py-4 text-green-600">执行新任务</td>
                            <td class="px-6 py-4 text-red-600">旧任务丢失</td>
                        </tr>
                        <tr>
                            <td class="px-6 py-4 whitespace-nowrap font-medium text-gray-900">自定义策略</td>
                            <td class="px-6 py-4 text-gray-500">自定义处理</td>
                            <td class="px-6 py-4 text-gray-500">特殊业务需求</td>
                            <td class="px-6 py-4 text-green-600">灵活定制</td>
                            <td class="px-6 py-4 text-red-600">开发成本</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>

        <!-- Best Practices -->
        <div class="bg-white p-8 rounded-xl shadow-sm">
            <h2 class="text-2xl font-bold mb-6 font-serif text-gray-800">最佳实践建议</h2>
            <div class="space-y-4">
                <div class="flex items-start">
                    <div class="flex-shrink-0 h-10 w-10 rounded-full bg-indigo-100 flex items-center justify-center text-indigo-600 mr-4">
                        <i class="fas fa-lightbulb"></i>
                    </div>
                    <div>
                        <h3 class="text-lg font-medium text-gray-900">评估任务重要性</h3>
                        <p class="mt-1 text-gray-600">根据任务对业务的重要性选择合适的策略，关键任务应考虑使用AbortPolicy或自定义策略确保处理。</p>
                    </div>
                </div>
                <div class="flex items-start">
                    <div class="flex-shrink-0 h-10 w-10 rounded-full bg-indigo-100 flex items-center justify-center text-indigo-600 mr-4">
                        <i class="fas fa-chart-line"></i>
                    </div>
                    <div>
                        <h3 class="text-lg font-medium text-gray-900">监控系统负载</h3>
                        <p class="mt-1 text-gray-600">高负载环境中，考虑使用DiscardOldestPolicy或CallerRunsPolicy来平衡系统资源。</p>
                    </div>
                </div>
                <div class="flex items-start">
                    <div class="flex-shrink-0 h-10 w-10 rounded-full bg-indigo-100 flex items-center justify-center text-indigo-600 mr-4">
                        <i class="fas fa-bell"></i>
                    </div>
                    <div>
                        <h3 class="text-lg font-medium text-gray-900">异常处理机制</h3>
                        <p class="mt-1 text-gray-600">使用AbortPolicy时，确保有完善的异常处理机制来捕获和处理RejectedExecutionException。</p>
                    </div>
                </div>
                <div class="flex items-start">
                    <div class="flex-shrink-0 h-10 w-10 rounded-full bg-indigo-100 flex items-center justify-center text-indigo-600 mr-4">
                        <i class="fas fa-project-diagram"></i>
                    </div>
                    <div>
                        <h3 class="text-lg font-medium text-gray-900">自定义策略灵活性</h3>
                        <p class="mt-1 text-gray-600">对于复杂业务场景，自定义拒绝策略可以提供最大的灵活性，如将任务持久化到数据库或消息队列。</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>